<?PHP
$PurviewLevel=0;
require("../../inc/common.inc.php");
require_once(dirname(__FILE__)."/../"."Admin_ChkPurview.php");
/**
*处理添加新消息

HTML信息必须保存到素材库方可调用
**/
if($action=="saveadd"){
$appuserstr="";
if(isset($_COOKIE["appusers"])){
	$appuserstr = $_COOKIE["appusers"];
}
if ($ttitle=="" || empty($ttitle)){
	$fun->popmassage("信息标题不能为空","","popback");
	exit();
}
$ttype = intval($ttype);
$tsucaiid = intval($tsucaiid);
$issave = intval($issave);
if($ttype==2){
	if($tcontent==""){
	$fun->popmassage("请设置消息的内容","","popback");
	exit();
	}
}elseif($ttype==1){
	if($thtml==""){
	$fun->popmassage("请设置消息的内容","","popback");
	exit();
	}
}else{
	if($turl==""){
	$fun->popmassage("请设置推送的链接","","popback");
	exit();
}
}
$addtime = mktime();
if(($issave==1||$ttype==1)&&$tsucaiid==0){
		$db->excu("insert into mx_sucai_app ( `atitle`, `atype`,`ahtml`,`aurl`,`addtime`,`addid`) VALUES ('$ttitle','$ttype','$thtml','$turl','$addtime','".$_SESSION['mxadmin']['userid']."')");
		$tsucaiid = mysql_insert_id();
}


$tgroupid=intval($tgroupid);
$tsex=intval($tsex);
$truepath=preg_replace("/(.+)\/([^\/]+)\/([^\/]+)/is","\\1",'http://'.$HTTP_HOST.$PHP_SELF);
		
$db->excu("INSERT INTO `mx_tuisong_app` ( `ttitle`, `ttype`,`tsucaiid`,`tcontent`,`turl`,`tuserids`,`tgroupid`,`tsex`,`tprovince`,`tcity`,`tarea`,`addtime`,`addid`) VALUES ('$ttitle','$ttype','$tsucaiid','$tcontent','$turl','$appuserstr','$tgroupid','$tsex','$province','$city','$area','$addtime','".$_SESSION['mxadmin']['userid']."')");
$tid = mysql_insert_id();

setcookie("appusers","",time()-3600);
setcookie("appusersnum","0",time()-3600);

if($appuserstr!=""){
	$appuserstr = substr($appuserstr,0,strlen($appuserstr)-1);
}else{
$wherestr="isok=1";
if($tprovince!=""){
$wherestr .= "and province like '%$tprovince%'";
}
if($tcity!=""){
$wherestr .= "and city like '%$tcity%'";
}
if($tarea!=""){
$wherestr .= "and district like '%$tarea%'";
}
if($tgroupid!=0){
	$wherestr.=" and groupid='$tgroupid'";
}
if($tsex!=2){
	$wherestr.=" and sex='$tsex'";
}
$userquery = $db->excu("select * from mx_app_user where ".$wherestr);
$userinfo = array();
while($userarr = $db->fetch_array($userquery)){
	$userinfo[] = $userarr;
}
if(count($userinfo)==0){
	$db->excu("delete from `mx_tuisong_app` where `tid`='$tid'");
	$fun->popmassage("您设置的条件未找到符合要求的用户","","popback");
	exit();
}else{
	$appuserstr="";
	foreach($userinfo as $key=>$value){
		if($key==0){
			$appuserstr.=$value["id"];
		}else{
			$appuserstr.=",".$value["id"];
		}
	}
}

}
if($tsucaiid!=0){
$sucaiinfo = $db->getinfo("select * from mx_sucai_app where aid ='$tsucaiid' limit 1");
if($ttype==1){
	$msg_content="url-:".$webdb["weburl"]."/".apppushurl."?id=".$tsucaiid;
}elseif($ttype==2){
	$msg_content="txt-:".$ttitle."：".$sucaiinfo["acontent"];	
}elseif($ttype==3){
	$msg_content="url-:".$sucaiinfo["aurl"];
}
}else{
	if($ttype==2){
		$msg_content="txt-:".$ttitle."：".$tcontent;	
	}else{
		$msg_content="url-:".$turl;
	}
}

$msg_content1 = array("n_content"=>$msg_content,"n_extras"=>array("ios"=>array("badge"=>1)));
$msg_content1 = json_encode($msg_content1);
$platform1 = "ios";

$msg_content2 = array("message"=>$msg_content);
$msg_content2 = json_encode($msg_content2);
$platform2 = "android";


$obj = new jpush(masterSecret,appkeys);			 
$res1 = $obj->send($tid, 3, $appuserstr, 1, $msg_content1, $platform1);
$res2 = $obj->send($tid, 3, $appuserstr, 2, $msg_content2, $platform2);
//	echo $res1["errcode"];
//echo $res2["errcode"];
//exit;
if($res2["errcode"]==0&&$res2["errcode"]==0){
	$db->excu("update `mx_tuisong_app` set isok=1 where tid='$tid'");
}
jump2("操作成功","tuisong_app_list.php",2);
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<TITLE>铭讯EOS网站后台管理</TITLE>
<Meta name="Keywords" Content="济南网站建设 济南网站制作 山东网站建设 济南铭讯软件有限公司">
<Meta name="Description" Content="济南网站制作：铭讯软件专业服务优秀企业，致力于网站建、设网站推广、网络营销一体的全访位网络服务商">
<link href="../images/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php require("../mx_head.php") ;?>

	<!--right-->
	<div class="right">
	  
		<div class="xm2">
			<h1 class="fl">新建推送</h1>
			<!--<span class="fr"><a href="#"><img src="../images/right_4.png" alt="设置" /></a></span>-->
			<div class="clear"></div>
		</div>
		<div class="xm2_b">
			<div class="height15"></div>
				<script type="text/javascript" src="../js/jquerys-1.5.1.js"></script>
                <script type="text/javascript" src="../js/pcasunzip.js"></script>
                <script type="text/javascript" src="../js/tuisong.js"></script>
				<script language="javascript">
				$(document).ready(function(){ //这个就是传说的ready   
				$(".stripe tr").mouseover(function(){   
				//如果鼠标移到class为stripe的表格的tr上时，执行函数   
				$(this).addClass("over");}).mouseout(function(){   
				//给这行添加class值为over，并且当鼠标一出该行时执行函数   
				$(this).removeClass("over");}) //移除该行的class   
				$(".stripe tr:even").addClass("alt");   
				//给class为stripe的表格的偶数行添加class值为alt
				//www.divcss5.com 整理特效
				});  
				function showinfo(returnid,changeid,changevalue){
					document.getElementById(changeid).value=changevalue;
					$.ajax({ url: "getinfo4.php?id="+changevalue+"&cid="+changeid,dataType: "json",success: function(msg){
					  if(msg.atype==1){
						  document.getElementById("ttitle").value=msg.atitle;
						  document.getElementById("ttitle").style.color='#000000';
						  editor.html(msg.ahtml);
						  document.getElementById("thtml").value=msg.ahtml;
					  }else{
						  document.getElementById("ttitle").value=msg.atitle;
						  document.getElementById("ttitle").style.color='#000000';
						  document.getElementById("turl").value=msg.aurl;
					  }
					}});

					
				}

 
var maxstrlen=140;
 
function Q(s){return document.getElementById(s);}
 
function checkWord(c){

var str = c.value;
 
myLen=getStrleng(str); 
 
len=maxstrlen;
 
var str = c.value;
 
myLen=getStrleng(str);
 
var wck=Q("wordCheck");
 
if(myLen>len*2){
 
c.value=str.substring(0,i+1);
 
}
 
else{
 
wck.innerHTML = Math.floor((len*2-myLen)/2);
 
}
 
}
 
function getStrleng(str){
 
myLen =0;
 
i=0;
 
for(;(i<str.length)&&(myLen<=maxstrlen*2);i++){
 
if(str.charCodeAt(i)>0&&str.charCodeAt(i)<128)
 
myLen++;
 
else
 
myLen+=2;
 
}
 
return myLen;
 
}
 


				function checkform(){
					var ttitle,ttype,tsucaiid,tmsgid;
					ttitle = document.form2.ttitle.value;
					ttype = document.form2.ttype.value;
					tsucaiid = document.form2.tsucaiid.value;
					tcontent = document.form2.tcontent.value;
					thtml = document.form2.thtml.value;
					turl = document.form2.turl.value;
					
					if(ttitle==""||ttitle=="请输入信息标题，字数20字以内"){
						alert("信息标题不能为空");
						document.form2.ttitle.focus();
						return false;
					}
					if(ttype==2){
						if(tcontent==""){
							alert("请设置消息的内容");
							return false;
						}
					}else if(ttype==1){
						if(thtml==""){
							alert("请设置消息的内容");
							return false;
						}
					}else{
						if(turl==""){
							alert("请设置推送的链接");
							return false;
						}
					}
					
					if(ttype==3&&tsucaiid==0){
						if(confirm("是否要将该链接保存到APP库？")){
							document.getElementById("issave").value=1;
						}
					}
					return true;
			    }
				function changeselectinfo(num){
					if(num==0){
						document.getElementById("selectinfo").innerHTML="";
					}else{
						document.getElementById("selectinfo").innerHTML="已手动选择"+num+"人";
					}
				}
				</script>
				<form id="form2" name="form2" method="post" action="?action=saveadd" onsubmit="return checkform()">
                <input type="hidden" name="ttype" id="ttype" value="2">
                <input type="hidden" name="tsucaiid" id="tsucaiid" value="0">
                <input type="hidden" name="issave" id="issave" value="0">
				  <table width="100%" border="0" align="left" cellpadding="9" cellspacing="0">
                    
                    <tr>
                      <td width="143" align="right" class="font14">发送对象：</td>
                      <td colspan="2" class="font14">
                      <select name="tgroupid">
                      <option value="0">全部用户</option>
                      <?php
                      $groupquery = $db->excu("select * from mx_app_user_group order by orderid,id");
					  while($grouparr = $db->fetch_array($groupquery)){
						 echo  '<option value="'.$grouparr["id"].'">'.$grouparr["groupname"].'</option>';
					  }
					  ?>
                      </select>
                      &nbsp;&nbsp;性别：<select name="tsex"><option value="2">全部</option><option value="1">男</option><option value="0">女</option></select>&nbsp;&nbsp;<input type="button" class="button2" name="button2" id="button2" value="手动选择" onclick="javascript:showdiv('list2','tsucaiid','tuisong_app_user','用户列表')" />&nbsp;&nbsp;<span id="selectinfo"><?php echo isset($_COOKIE["appusersnum"])?($_COOKIE["appusersnum"]!=0?"已手动选择".$_COOKIE["appusersnum"]." 人":""):"";?></span></td>
                    </tr>
                    <tr>
                      <td width="143" align="right" class="font14">发送地区：</td>
                      <td colspan="2" class="font14">
                      <select name="province"></select>&nbsp;&nbsp;&nbsp;&nbsp;<select name="city"></select>&nbsp;&nbsp;&nbsp;&nbsp;<select name="area"></select></td>
                    </tr>
                    <tr>
                      <td align="right" class="font14">信息标题：</td>
                      <td colspan="2"><input type="text" name="ttitle" id="ttitle" size="70" maxlength="20"  onFocus="if (this.value=='请输入信息标题，字数20字以内'){style.color='#000000';this.value='';}" onBlur="if (this.value==''){this.style.color='#cfcfcf';this.value='请输入信息标题，字数20字以内';}" value="请输入信息标题，字数20字以内" style="color:#cfcfcf; margin-top:1px;" /></td>
                    </tr>
                    <script>
					new PCAS("province","city","area");
					</script>
                    <script>
					function switchmod(modtag,modtagclass1,modtagclass2,modk,modknum)
					{
						document.getElementById("ttype").value=modk;
						document.getElementById("tsucaiid").value=0;
						editor.html("");
						document.getElementById("thtml").value="";
						document.getElementById("turl").value="";
						
						for(i=1; i <=modknum; i++)
						{
							content=modtag+"_list"+i;
							if (i==modk)
							{
							document.getElementById(modtag+i).className=modtagclass1;
							document.getElementById(content).style.display="block";
							}
							else
							{
							document.getElementById(modtag+i).className=modtagclass2;
							document.getElementById(content).style.display="none";
							}
						}
					}
					
					</script>
                    <tr>
                      <td align="right" class="font14">信息类型：</td>
                      <td colspan="2">
                      <input type="button" class="button" value="文字" id="tab_2" onclick="switchmod('tab_','button','button2',2,3)">&nbsp;
                      <input type="button" class="button2" value="HTML" id="tab_1" onclick="switchmod('tab_','button','button2',1,3)">&nbsp;
                      <!--<input type="button" class="button2" value="文字" id="tab_2" onclick="switchmod('tab_','button','button2',2,3)">&nbsp;-->
                      <input type="button" class="button2" value="链接" id="tab_3" onclick="switchmod('tab_','button','button2',3,3)">
                      </td>
                    </tr>
                    <tr>
                      <tr><td colspan="3" height="0">
                       <div id="tab__list1" style="display:none;">
                    <table width="100%" border="0" align="left" cellpadding="9" cellspacing="0">
                    <tr>
                      <td width="134" align="right" class="font14">&nbsp;</td>
                      <td colspan="2"><a href="javascript:showdiv('list1','tsucaiid','tuisong_appk_list','HTML信息')">点击选取HTML信息</a></td>
                    </tr>
                    <tr>
                      <td width="134" align="right" class="font14">信息内容：</td>
                      <td colspan="2">
                      <script charset="utf-8" src="/webedito/kindeditor-min.js"></script>
					  <script charset="utf-8" src="/webedito/lang/zh_CN.js"></script>
                      <script>
                      var editor;
                      KindEditor.ready(function(K) {
                          editor = K.create('textarea[id="thtml"]', {
                            allowFileManager : true,
                            filterMode : false,
                            afterBlur : function() {this.sync();},

						items : ['bold','italic', 'underline', '|','insertorderedlist','insertunorderedlist','|','image','|','removeformat','forecolor','hilitecolor']
                          });
                      });
                      </script>
                      <textarea name="thtml" id="thtml" style="width:510px;height:300px;visibility:hidden;display:none;"></textarea>
                      </td>
                    </tr>
                    </table>
                    
                    </div>
                      <!--<div id="tab__list2" style="display:none;">
                      <p class="abutton"><a href="javascript:showdiv('list2','tsucaiid','tuisong_appk_list','文字信息')">点击选取文字信息</a></p>
                      <div id="list2" class="tab_box">
                      </div>
                      </div>-->
                      <div id="tab__list2">
                       <table width="100%" border="0" align="left" cellpadding="9" cellspacing="0">
                    <!--<tr>
                      <td width="134" align="right" class="font14">&nbsp;</td>
                      <td colspan="2"><a href="javascript:showdiv('tab__list2','tsucaiid','tuisong_music_list','HTML信息')">点击选取HTML信息</a></td>
                    </tr>-->
                    <tr>
                      <td width="134" align="right" class="font14">信息内容：</td>
                      <td colspan="2">
                      <textarea name="tcontent" id="tcontent" cols="70" rows="15" onKeyUp="javascript:checkWord(this);" onMouseDown="javascript:checkWord(this);"></textarea>
                      <p>还可以输入<span style="font-family: Georgia; font-size: 26px;" id="wordCheck">140</span>个字符</p>
                      </td>
                    </tr>
                    </table>
                      </div>
                      <div id="tab__list3" style="display:none;">
                    <table width="100%" border="0" align="left" cellpadding="9" cellspacing="0">
                    <tr>
                      <td width="134" align="right" class="font14">&nbsp;</td>
                      <td colspan="2"><a href="javascript:showdiv('list3','tsucaiid','tuisong_appk_list','HTML信息')">点击选取HTML信息</a></td>
                    </tr>
                    <tr>
                      <td width="134" align="right" class="font14">推送链接：</td>
                      <td colspan="2">
                      <input type="text" name="turl" id="turl" size="70" value="" />
                      </td>
                    </tr>
                    </table>
                    
                    </div>
                      
                      </td>
                    </tr>
                    
                    
                    <tr>
                      <td>&nbsp;</td>
                      <td class="font14" colspan="2"><input type="submit" class="button" name="button" id="button" value="完成" />&nbsp;&nbsp;
                      <input type="reset" class="button2" name="button2" id="button2" value="取消" /></td>
                    </tr>
                    <tr>
                      <td colspan="3" height="20">&nbsp;</td>
                    </tr>
                  </table>
          </form>
				<div class="height15"></div>
	  </div>
		<div class="clear"></div>
		<div class="height15"></div>
       
	  </div>
		<div class="clear"></div>
  <?php require("../mx_foot.php") ;?>
  </div>
	<!--right-end-->
</body>
</html>
